//https://www.jianshu.com/p/fbcf44b243a8
var mssql = require('mssql');

var db = {};
var config = {
  user: 'sa',
  password: '123456',
  server: '127.0.0.1', 
  database: 'qiyu',
  port:1433,
  options: {
    encrypt: false // Use this if you're on Windows Azure
  },
  pool: {
    min: 0,
    max: 10,
    idleTimeoutMillis: 3000
  }
};
 
//执行sql,返回数据.
db.sql = function (sql, callBack) {
  console.log("sql", sql)
  var connection = new mssql.ConnectionPool(config, function (err) {
    if (err) {
      console.log(err);
      return;
    }
    var ps = new mssql.PreparedStatement(connection);
    ps.prepare(sql, function (err) {
      if (err){
        console.log(err);
        return;
      }
      ps.execute('', function (err, result) {
        if (err){
          console.log(err);
          return;
        }
 
        ps.unprepare(function (err) {
          if (err){
            console.log(err);
            callback(err,null);
            return;
          }
            callBack(err, result);
        });
      });
    });
  });
};
 
// let girl =   db.sql('select * from girl', function(err, result) {
//   if(err) {
//       console.log("sql错误", err);
//       return;
//   }
//   console.log(result)
  
//   return result;

// });
// console.log(girl)

let girl2 = mssql.connect(config).then(pool => {
  // Query
  
  return pool.request()
      // .input('input_parameter', sql.Int, value)
      .query('select * from girl')
}).then(result => {
  // console.log(result)
  return result;
}).catch(err => {
  console.log(err)
// ... error checks
});
console.log(girl2)
module.exports = db;